import { BtMessage } from "./utils/BtMessage.js"
import { BtMessageBox } from "./utils/BtMessageBox.js"
import { BtNotification } from "./utils/BtNotification.js"

import { getAuthInfo, updateInfo } from './api/index.js'
import { useAuthStore } from "./store/authStore.js"

import SHA256 from "./utils/sha256.js"


// 获取用户信息
const auth = useAuthStore()

const barHandler = setInterval(() => {
    refreshBarPosition()
}, 10)

// 更新用户信息到页面
const refresUserInfo = () => {
    $("#i-img").attr("src", auth.imgUrl)
    $("#i-name").text(auth.name)
    $("#i-signature").text(auth.signature || '该用户只想抽个时间睡大觉')
    $("#i-c-like").text(auth.likeCount)
    $("#i-learnTime").text(auth.learnTime + "H")
    $("#i-c-report").text(auth.reportCount)
    $("#i-email").text(auth.email)
}



const refreshBarPosition = () => {
    const item = $(".u-tabs .nav-item .active").parent()

    let { left, top } = item.offset()

    top += Math.ceil(item.height())
    $(".u-tabs-bar").css("top", top).css("left", Math.floor(left)).css("width", item.width())
}

$(document).ready(() => {
    // 校验用户是否登录
    getAuthInfo().then(res => {
        if (res.code != 200) {
            console.log("token 鉴权失败")
            BtMessage({
                type: 'error',
                message: res.msg
            })
            setTimeout(() => {
                window.location.href = '/login.html'
            }, 1000)
            return
        }
        // 否则将auth加载到页面
        refresUserInfo()
    })
    refreshBarPosition()
})


$("#my-form").submit(function (e) {

    e.preventDefault()

    const btn = $("#my-form-submit")
    // if (btn.hasClass("disabled"))
    btn.attr("disabled", true).children(".spinner-border").removeClass("d-none")

    // 假装在提交
    BtNotification({
        type: 'info',
        title: 'wating..',
        message: '正在保存数据...稍安勿躁'
    })

    const item = $(this)
    // 构造信息


    const params = {
        name: item.find("#u-m-name").val(),
        email: item.find("#u-m-email").val(),
        signature: item.find("#u-m-sign").val(),
        gender: item.find("#u-m-gender .custom-control-input:checked").val(),
        password: item.find("#u-m-pwd").val()
    }

    if (params.password) {
        params.password = SHA256(params.password)
    }

    console.log("modify params", params)

    updateInfo(params).then(res => {
        if (res.code != 200) {
            BtNotification({
                type: 'error',
                title: '啊哦，失败了...',
                message: res.msg || '失败了，但是不知道是什么原因'
            })
            return
        }

        getAuthInfo().then(res => {
            // 懒得判断了
            setTimeout(() => {
                // window.location.reload()
                refresUserInfo()
                BtNotification({
                    type: 'success',
                    title: 'success',
                    message: '修改个人信息成功!'
                })
                btn.removeAttr("disabled").children(".spinner-border").addClass("d-none")
        
            }, 500)
        })
    })

    

    console.log($(this))

    

})

$("#notice-read").click(function() {
    $(".u-notice .media").addClass("n-readed").find(".badge").fadeOut(100)
    BtMessage({
        type: 'success',
        message: '已完成一目百行!'
    })
})
$("#notice-clean").click(function() {
    BtMessageBox({
        type: 'warning',
        title: '询问',
        message: '确定要清空所有已读消息吗?',
        showCancelButton: true,
        onClose: (type) => {
            // 模拟清空
            $(".u-notice .media").filter(".n-readed").remove()
            if (type == 'confirm') {
                BtMessage({
                    type: 'success',
                    message: '消息清空完成!'
                })
            }
            if ($(".u-notice .media").length == 0) {
                $(".u-notice .n-empty").removeClass("d-none")
            } 

        }
    })
})

$(".u-notice .media").click(function() {
    const item = $(this)
    BtMessageBox({
        type: 'info',
        title: '【系统通知】',
        message: '恭喜你啊年轻人，你成功的被我恭喜到了。',
        confirmText: '好的',
        onClose: () => {
            item.find(".badge").fadeOut(100, () => {
                item.addClass("n-readed")
            })
        }
    })
})


$("#u-like").click(function() {
    $(this).removeClass("btn-success").addClass("btn-light").attr("disabled", "ture").text("已经赞过了")
})